package web;

import entity.Msg;
import entity.Reservation;
import entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import service.ReservationService;

import java.util.Date;
import java.util.List;
import java.util.Map;

/***
 * Author: Mr.Zhao, Time: 2019/5/23/16:50
 * Description: 有关预约的所有操作
 */
@Controller
public class ReservationController {

    @Autowired
    ReservationService reservationService;


    @RequestMapping(value = "/test2")
    @ResponseBody
    public Msg test() {
        return Msg.success();
    }

    @RequestMapping(value = "/test2", params = "t1")
    @ResponseBody
    public Msg test(@RequestParam(value = "t1") String t1) {
        return Msg.success().add("t1", t1);
    }

    @RequestMapping(value = "/test2", params = {"t1", "t2"})
    @ResponseBody
    public Msg test(@RequestParam(value = "t1") String t1, @RequestParam(value = "t2") String t2) {
        return Msg.success().add("t1", t1).add("t2", t2);
    }


    /**
     * 根据所选时间查询预约记录
     *
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     */
    @RequestMapping(value = "/reservation_time", method = RequestMethod.POST)
    @ResponseBody
    public Msg queryByTime(@RequestParam(value = "starttime") Date startTime, Date endTime) {
        return Msg.success();
    }


    /**
     * 进行预约，即添加预约记录
     * 或更新预约记录
     *
     * @param reservation 传来的预约实体
     * @return
     */
    @RequestMapping(value = "/reservation", method = RequestMethod.PUT)
    @ResponseBody
    public Msg insertOrUpdateReservation(@RequestBody Reservation reservation) {

        try {
            //插入记录
            if (reservation.getId() == null) {
                //设置预约为未审核状态
                reservation.setStatus(0);
                reservationService.addReservation(reservation);
            } else //更新记录
            {

                reservationService.update(reservation);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Msg.fail().setMsg("操作失败");
        }
        return Msg.success();
    }

    /**
     * 根据用户查找记录，按预约开始时间降序排列
     *
     * @param user 传来的预约实体
     * @return
     */
    @RequestMapping(value = "/reservation", method = RequestMethod.POST)
    @ResponseBody
    public Msg insertOrUpdateReservation(@RequestBody User user) {
        try {
            List<Reservation> reservations = reservationService.selectByUsername(user.getUsername());
            return Msg.success().add("reservations", reservations);
        } catch (Exception e) {
            e.printStackTrace();
            return Msg.fail().setMsg("查询失败");
        }
    }

    //TODO:增加管理员验证

    /**
     * 查询未审核的预约记录，用于审核管理员
     * 不返回已过期的预约记录
     *
     * @return 预约记录实体集
     */
    @RequestMapping(value = "/reservation/not_audited", method = RequestMethod.GET)
    @ResponseBody
    public Msg getNotAuditedReservation() {
        try {
            List<Reservation> reservations = reservationService.selectNotAuditedReservation();
            return Msg.success().add("reservations", reservations);
        } catch (Exception e) {
            return Msg.fail().setMsg("查询失败！");
        }
    }

    /**
     * 查询已审核的预约记录
     *
     * @return 预约记录实体集
     */
    @RequestMapping(value = "/reservation/audited", method = RequestMethod.GET)
    @ResponseBody
    public Msg getAuditedReservation() {
        try {
            List<Reservation> reservations = reservationService.selectAuditedReservation();
            return Msg.success().add("reservations", reservations);
        } catch (Exception e) {
            return Msg.fail().setMsg("查询失败！");
        }
    }

    /**
     * 根据教室id查询预约记录
     *
     * @return
     */
    @RequestMapping(value = "/reservation", params = {"classroom_id"}, method = RequestMethod.POST)
    @ResponseBody
    public Msg getReservationByClassroom(@RequestParam(value = "classroom_id") Integer classroomId) {
        try {
            List<Reservation> reservations = reservationService.selectByClassroomId(classroomId);
            return Msg.success().add("reservations", reservations);
        } catch (Exception e) {
            return Msg.fail().setMsg("查询失败！");
        }
    }

}
